package org.aynu.propertyservice.service.impl;

import org.aynu.propertyservice.common.PropertyResponse;
import org.aynu.propertyservice.domain.RepairRequestsPo;
import org.aynu.propertyservice.mapper.RepairRequestsMapper;
import org.aynu.propertyservice.service.IRepairRequestsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

/**
 * <p>
 * 维修请求表 服务实现类
 * </p>
 *
 * @author 小粥
 * @since 2025-03-03
 */
@Service
public class RepairRequestsServiceImpl extends ServiceImpl<RepairRequestsMapper, RepairRequestsPo> implements IRepairRequestsService {

    @Override
    public PropertyResponse<List<RepairRequestsPo>> selectResident(Integer userId) {
        List<RepairRequestsPo> repairRequestsPos = lambdaQuery()
                .eq(RepairRequestsPo::getUserId, userId)
                .select(
                        RepairRequestsPo::getRequestId,
                        RepairRequestsPo::getUserId,
                        RepairRequestsPo::getType,       // 新增
                        RepairRequestsPo::getDescription,
                        RepairRequestsPo::getPhone,      // 新增
                        RepairRequestsPo::getStatus,
                        RepairRequestsPo::getAssignedTo,
                        RepairRequestsPo::getResolvedAt,
                        RepairRequestsPo::getCreatedAt,
                        RepairRequestsPo::getUpdatedAt
                )
                .list();
        return PropertyResponse.success(repairRequestsPos, "所有用户已存在该年份缴费记录");
    }

    @Override
    public PropertyResponse<Integer> updateRequest(Integer requestId, String status, Integer assignedTo, Timestamp resolvedAt) {
        boolean update = lambdaUpdate()
                .eq(RepairRequestsPo::getRequestId, requestId)
                .set(!("待处理".equals(status)), RepairRequestsPo::getStatus, status)
                .set(assignedTo != null, RepairRequestsPo::getAssignedTo, assignedTo)
                .set(status.equals("已完成"), RepairRequestsPo::getResolvedAt, resolvedAt)
                .update();
        return PropertyResponse.success(update ? 1 : 0, "所有用户已存在该年份缴费记录");
    }
}